import unittest
from HTMLTestRunner import HTMLTestRunner
import os
import time

# loader 加载器
loader = unittest.defaultTestLoader.discover('testcase', 'demo*')

# runner
# 准备要写入的文件
# 存放报告的文件路径
report = os.path.abspath('report')
# 文件名规则:年月日时分秒-项目.html
filename = time.strftime('%Y%m%d%H%M%S') + '-ecshop.html'
# 拼接文件路径
filepath = os.path.join(report, filename)

# 写入文件,执行测试用例,生成测试报告
with open(filepath, 'wb') as f:
    runner = HTMLTestRunner(
        title='测试报告标题',
        stream=f,
        verbosity=2
    )
    runner.run(loader)